<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>Spring Web MVC相关注解 | 奔跑的蜗牛 | 虚心若愚</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="SpringMVC">
    <meta name="description" content="Spring Web MVC相关注解@Controller控制器定义 和Struct1一样，Spring的Controller是Singleton的。这就意味着会被许多个请求线程共享。因此，我们将控制器设计成无状态类。 在Spring 3.0中，通过@Controoler标注可将class定义为一个Controller类。为使Spring能找到定义为Controller的bean，需要在sprin">
<meta name="keywords" content="SpringMVC">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Web MVC相关注解">
<meta property="og:url" content="http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/index.html">
<meta property="og:site_name" content="奔跑的蜗牛">
<meta property="og:description" content="Spring Web MVC相关注解@Controller控制器定义 和Struct1一样，Spring的Controller是Singleton的。这就意味着会被许多个请求线程共享。因此，我们将控制器设计成无状态类。 在Spring 3.0中，通过@Controoler标注可将class定义为一个Controller类。为使Spring能找到定义为Controller的bean，需要在sprin">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-03-18T04:06:59.891Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Web MVC相关注解">
<meta name="twitter:description" content="Spring Web MVC相关注解@Controller控制器定义 和Struct1一样，Spring的Controller是Singleton的。这就意味着会被许多个请求线程共享。因此，我们将控制器设计成无状态类。 在Spring 3.0中，通过@Controoler标注可将class定义为一个Controller类。为使Spring能找到定义为Controller的bean，需要在sprin">
    
        <link rel="alternative" href="/atom.xml" title="奔跑的蜗牛" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@1.5.2/css/style.css">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/timg.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/user2.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">windy</h5>
          <a href="mailto:windynature@163.com" title="windynature@163.com" class="mail">windynature@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/WindyNature" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/windynature" target="_blank" >
                <i class="icon icon-lg icon-mayun"></i>
                码云
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Spring Web MVC相关注解</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Spring Web MVC相关注解</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-01-25T15:19:06.000Z" itemprop="datePublished" class="page-time">
  2017-01-25
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/Java-Web/">Java Web</a></li></ul>

            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Spring-Web-MVC相关注解"><span class="post-toc-text">Spring Web MVC相关注解</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Controller控制器定义"><span class="post-toc-text">@Controller控制器定义</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#RequestMapping"><span class="post-toc-text">@RequestMapping</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#RequestParam"><span class="post-toc-text">@RequestParam</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#SessionAttributes"><span class="post-toc-text">@SessionAttributes</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#ModelAttribute"><span class="post-toc-text">@ModelAttribute</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Controller类中方法返回值的处理"><span class="post-toc-text">Controller类中方法返回值的处理</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#获得request对象、session对象"><span class="post-toc-text">获得request对象、session对象</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#ModelMap"><span class="post-toc-text">ModelMap</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#ModelAndView模型视图类"><span class="post-toc-text">ModelAndView模型视图类</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-java/springmvc相关注解"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Spring Web MVC相关注解</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年01月25日 23:19" datetime="2017-01-25T15:19:06.000Z"  itemprop="datePublished">2017-01-25</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/Java-Web/">Java Web</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="Spring-Web-MVC相关注解"><a href="#Spring-Web-MVC相关注解" class="headerlink" title="Spring Web MVC相关注解"></a>Spring Web MVC相关注解</h1><h2 id="Controller控制器定义"><a href="#Controller控制器定义" class="headerlink" title="@Controller控制器定义"></a>@Controller控制器定义</h2><hr>
<p>和<code>Struct1</code>一样，<code>Spring</code>的<code>Controller</code>是<code>Singleton</code>的。这就意味着会被许多个请求线程共享。因此，我们将控制器设计成无状态类。</p>
<p>在<code>Spring 3.0</code>中，通过<code>@Controoler</code>标注可将class定义为一个<code>Controller</code>类。为使<code>Spring</code>能找到定义为<code>Controller</code>的<code>bean</code>，需要在<code>spring-context</code>配置文件中增加如下定义：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.sxt.web"</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<p>注：实际上，使用<code>@Component</code>，也可以气到<code>@Controller</code>同样的作用。</p>
<h2 id="RequestMapping"><a href="#RequestMapping" class="headerlink" title="@RequestMapping"></a>@RequestMapping</h2><hr>
<p>在类前面定义，则将url和类绑定。<br>在方法前面定义，则将url和类的方法绑定，如下所示。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sxt.web;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> com.sxt.service.UserService;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Resource</span></span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg</span><span class="params">(String uname)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"HelloController.HandleRequest()"</span>);</span><br><span class="line">        userService.add(uname);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">    &#125;[mybatis3动态创建表](media/mybatis3%E5%<span class="number">8</span>A%A8%E6%<span class="number">80</span>%<span class="number">81</span>%E5%<span class="number">88</span>%<span class="number">9</span>B%E5%BB%BA%E8%A1%A8-<span class="number">1</span>.markdown)</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserService <span class="title">getUservice</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userService;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserService</span><span class="params">(UserService userService)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.userSErvice = userService;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="RequestParam"><a href="#RequestParam" class="headerlink" title="@RequestParam"></a>@RequestParam</h2><hr>
<p>一般用于将指定的请求参数付给方法中形参。示例代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@RequestMappint</span>(params=<span class="string">"method=reg5"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">reg5</span><span class="params">(@RequestParam(<span class="string">"name"</span>)</span>String uname, ModelMap map) </span>&#123;</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="string">"HelloController.handleRequest()"</span>);</span><br><span class="line">    System.out.println(uname);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">&#125;[mybatis3动态创建表](media/mybatis3%E5%<span class="number">8</span>A%A8%E6%<span class="number">80</span>%<span class="number">81</span>%E5%<span class="number">88</span>%<span class="number">9</span>B%E5%BB%BA%E8%A1%A8.markdown)</span><br></pre></td></tr></table></figure>

<p>这样就把name参数的值付给uname。当然，如果请求参数名称和形参名称保持一致，则不需要这种写法。</p>
<h2 id="SessionAttributes"><a href="#SessionAttributes" class="headerlink" title="@SessionAttributes"></a>@SessionAttributes</h2><hr>
<p>将ModelMap中指定的属性放到Session中。示例代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Controoler</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="meta">@SessionAttributes</span>(&#123;<span class="string">"u"</span>,<span class="string">"a"</span>&#125;)   </span><br><span class="line"><span class="comment">//将ModelMap中属性名字为u、a的再放入session中。这样，request和session中都有了。</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg4"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg4</span><span class="params">(ModelMap map)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"HelloController.handleRequest()"</span>)；</span><br><span class="line">        <span class="comment">//将u放入request作用域中，这样转发页面也可以取到这个数据。</span></span><br><span class="line">        map.addAttribute(<span class="string">"u"</span>,<span class="string">"uuuu"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>*********$&#123;requestScope.u.uname&#125;<span class="tag">&lt;<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>*********$&#123;sessionScope.u.uname&#125;<span class="tag">&lt;<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注：名字为”user”的属性再结合使用注解@SessionAttributes可能会报错</p>
<h2 id="ModelAttribute"><a href="#ModelAttribute" class="headerlink" title="@ModelAttribute"></a>@ModelAttribute</h2><hr>
<p>这个注解可以跟@SessionAtributes配合在一起使用。可以将ModelMap中属性的值通过该注解自动复制给指定变量。<br>示例代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="meta">@SessionAttributes</span>(&#123;<span class="string">"u"</span>,<span class="string">"a"</span>&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg4"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg4</span><span class="params">(ModelMap map)</span> </span>&#123;</span><br><span class="line">        Sout.pringln(<span class="string">"HelloController.handlerRequest()"</span>);</span><br><span class="line">        map.addAttribute(<span class="string">"u"</span>,<span class="string">"尚学堂高琪"</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//将属性u的值赋给形参uname</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg5</span><span class="params">(@ModelAttribute(<span class="string">"u"</span>)</span>String uname,ModelMap map) </span>&#123;</span><br><span class="line">        System.out.pirntln(<span class="string">"HelloController.handleRequest"</span>);</span><br><span class="line">        System.out.println(uname);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注：先调用reg4方法，在调用reg5方法。控制台输出：尚学堂高琪</p>
<h2 id="Controller类中方法返回值的处理"><a href="#Controller类中方法返回值的处理" class="headerlink" title="Controller类中方法返回值的处理"></a>Controller类中方法返回值的处理</h2><hr>
<ol>
<li><p>返回String(建议)</p>
<ol>
<li><p>根据返回值找对应的显示页面。路径规则为：prefix前缀+返回值+suffix后缀组成</p>
</li>
<li><p>代码如下：</p>
 <figure class="highlight java"><table><tr><td class="code"><pre><span class="line">            <span class="function"><span class="keyword">public</span> String <span class="title">reg4</span><span class="params">(ModelMap map)</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"HelloController.handleReuest()"</span>);</span><br><span class="line">                <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        ``` </span><br><span class="line"><span class="number">2</span>. 也可以返回ModelMap、ModelAndView、Map、List、Set、Object、无返回值。一般建议返回字符串！</span><br><span class="line"></span><br><span class="line">## 请求转发和重定向</span><br><span class="line">---</span><br><span class="line">代码示例：</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg4"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg4</span><span class="params">(ModelMap map)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"HelloController.handleReuest"</span>);</span><br><span class="line">        <span class="comment">//reutrn "forward:index.jsp";</span></span><br><span class="line">        <span class="comment">//return "forward:user.do?method=reg5"; //转发</span></span><br><span class="line">        <span class="comment">//return "redirect:user.do?method=reg5";//重定向</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">"redirect:http://www.baidu.com"</span>; <span class="comment">//重定向</span></span><br><span class="line">    &#125;    </span><br><span class="line">    </span><br><span class="line">    <span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg5"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg5</span><span class="params">(String uname,ModelMap map)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"HelloController.handleRequest()"</span>);</span><br><span class="line">        System.out.println(uname);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
</li>
</ol>
<p>访问reg4方法，既可以看到效果。</p>
<h2 id="获得request对象、session对象"><a href="#获得request对象、session对象" class="headerlink" title="获得request对象、session对象"></a>获得request对象、session对象</h2><hr>
<p>普通的Controller类，示例代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg2"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg2</span><span class="params">(String uname, HttpServletRequest req, ModelMap map)</span> </span>&#123;</span><br><span class="line">        req.setAttribute(<span class="string">"a"</span>,<span class="string">"aa"</span>);</span><br><span class="line">        ret.getSession().setAttribute(<span class="string">"b"</span>,<span class="string">"bb"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="ModelMap"><a href="#ModelMap" class="headerlink" title="ModelMap"></a>ModelMap</h2><hr>
<p>ModelMap是map的实现，可以在其中存放属性，作用域同request。下面这个示例，我们可以在modelMap中存放数据，然后在forward页面上显示这些数据。通过el表达式、JSTL、java代码均可。代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">MutiActionController</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@ReuestMapping</span>(params=<span class="string">"method=reg"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">reg</span><span class="params">(String uname,ModelMap map)</span> </span>&#123;</span><br><span class="line">        map.put(<span class="string">"a"</span>,<span class="string">"aaa"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"index"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">%@</span> <span class="attr">page</span> <span class="attr">language</span>=<span class="string">"java"</span> <span class="attr">import</span>=<span class="string">"java.util.*"</span> <span class="attr">pageEncoding</span>=<span class="string">"gbk"</span>%&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">%@</span> <span class="attr">taglib</span> <span class="attr">prefix</span>=<span class="string">"c"</span> <span class="attr">uri</span>=<span class="string">"http://java.sun.com/jsp/jstl/core"</span>%&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">HTML</span> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//W3C//DTD HTML 4.01 Transitional//EN"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	   <span class="tag">&lt;<span class="name">h1</span>&gt;</span>$&#123;requestScope.a&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">	   <span class="tag">&lt;<span class="name">c:out</span> <span class="attr">value</span>=<span class="string">"$&#123;requestScope.a&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">c:out</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="ModelAndView模型视图类"><a href="#ModelAndView模型视图类" class="headerlink" title="ModelAndView模型视图类"></a>ModelAndView模型视图类</h2><hr>
<p>见名知意，从名字上我们可以知道ModelAndView中的Model代表模型，View代表视图。即，这个类把要显示的数据存放到Model属性中，要跳转的信息存储到啊了view属性。从ModelAndView的源代码可以知道其中的关系。部分源代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">	<span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ModelAndView</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/** View instance or view name String */</span></span><br><span class="line">	<span class="keyword">private</span> Object view;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/** Model Map */</span></span><br><span class="line">	<span class="keyword">private</span> ModelMap model;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Indicates whether or not this instance has been cleared with a call to &#123;<span class="doctag">@link</span> #clear()&#125;.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">boolean</span> cleared = <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Default constructor for bean-style usage: populating bean</span></span><br><span class="line"><span class="comment">	 * properties instead of passing in constructor arguments.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #setView(View)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #setViewName(String)</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Convenient constructor when there is no model data to expose.</span></span><br><span class="line"><span class="comment">	 * Can also be used in conjunction with &lt;code&gt;addObject&lt;/code&gt;.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> viewName name of the View to render, to be resolved</span></span><br><span class="line"><span class="comment">	 * by the DispatcherServlet's ViewResolver</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #addObject</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">(String viewName)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = viewName;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Convenient constructor when there is no model data to expose.</span></span><br><span class="line"><span class="comment">	 * Can also be used in conjunction with &lt;code&gt;addObject&lt;/code&gt;.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> view View object to render</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #addObject</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = view;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Creates new ModelAndView given a view name and a model.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> viewName name of the View to render, to be resolved</span></span><br><span class="line"><span class="comment">	 * by the DispatcherServlet's ViewResolver</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> model Map of model names (Strings) to model objects</span></span><br><span class="line"><span class="comment">	 * (Objects). Model entries may not be &lt;code&gt;null&lt;/code&gt;, but the</span></span><br><span class="line"><span class="comment">	 * model Map may be &lt;code&gt;null&lt;/code&gt; if there is no model data.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">(String viewName, Map&lt;String, ?&gt; model)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = viewName;</span><br><span class="line">		<span class="keyword">if</span> (model != <span class="keyword">null</span>) &#123;</span><br><span class="line">			getModelMap().addAllAttributes(model);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Creates new ModelAndView given a View object and a model.</span></span><br><span class="line"><span class="comment">	 * &lt;emphasis&gt;Note: the supplied model data is copied into the internal</span></span><br><span class="line"><span class="comment">	 * storage of this class. You should not consider to modify the supplied</span></span><br><span class="line"><span class="comment">	 * Map after supplying it to this class&lt;/emphasis&gt;</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> view View object to render</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> model Map of model names (Strings) to model objects</span></span><br><span class="line"><span class="comment">	 * (Objects). Model entries may not be &lt;code&gt;null&lt;/code&gt;, but the</span></span><br><span class="line"><span class="comment">	 * model Map may be &lt;code&gt;null&lt;/code&gt; if there is no model data.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">(View view, Map&lt;String, ?&gt; model)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = view;</span><br><span class="line">		<span class="keyword">if</span> (model != <span class="keyword">null</span>) &#123;</span><br><span class="line">			getModelMap().addAllAttributes(model);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Convenient constructor to take a single model object.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> viewName name of the View to render, to be resolved</span></span><br><span class="line"><span class="comment">	 * by the DispatcherServlet's ViewResolver</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> modelName name of the single entry in the model</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> modelObject the single model object</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">(String viewName, String modelName, Object modelObject)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = viewName;</span><br><span class="line">		addObject(modelName, modelObject);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Convenient constructor to take a single model object.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> view View object to render</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> modelName name of the single entry in the model</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> modelObject the single model object</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ModelAndView</span><span class="params">(View view, String modelName, Object modelObject)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = view;</span><br><span class="line">		addObject(modelName, modelObject);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Set a view name for this ModelAndView, to be resolved by the[mybatis3动态创建表](media/mybatis3%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BA%E8%A1%A8-2.markdown)</span></span><br><span class="line"><span class="comment">	 * DispatcherServlet via a ViewResolver. Will override any</span></span><br><span class="line"><span class="comment">	 * pre-existing view name or View.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setViewName</span><span class="params">(String viewName)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = viewName;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return the view name to be resolved by the DispatcherServlet</span></span><br><span class="line"><span class="comment">	 * via a ViewResolver, or &lt;code&gt;null&lt;/code&gt; if we are using a View object.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getViewName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">this</span>.view <span class="keyword">instanceof</span> String ? (String) <span class="keyword">this</span>.view : <span class="keyword">null</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Set a View object for this ModelAndView. Will override any</span></span><br><span class="line"><span class="comment">	 * pre-existing view name or View.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setView</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = view;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return the View object, or &lt;code&gt;null&lt;/code&gt; if we are using a view name</span></span><br><span class="line"><span class="comment">	 * to be resolved by the DispatcherServlet via a ViewResolver.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> View <span class="title">getView</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">this</span>.view <span class="keyword">instanceof</span> View ? (View) <span class="keyword">this</span>.view : <span class="keyword">null</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Indicate whether or not this &lt;code&gt;ModelAndView&lt;/code&gt; has a view, either</span></span><br><span class="line"><span class="comment">	 * as a view name or as a direct &#123;<span class="doctag">@link</span> View&#125; instance.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">hasView</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">this</span>.view != <span class="keyword">null</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return whether we use a view reference, i.e. &lt;code&gt;true&lt;/code&gt;</span></span><br><span class="line"><span class="comment">	 * if the view has been specified via a name to be resolved by the</span></span><br><span class="line"><span class="comment">	 * DispatcherServlet via a ViewResolver.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isReference</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">this</span>.view <span class="keyword">instanceof</span> String);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return the model map. May return &lt;code&gt;null&lt;/code&gt;.</span></span><br><span class="line"><span class="comment">	 * Called by DispatcherServlet for evaluation of the model.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">protected</span> Map&lt;String, Object&gt; <span class="title">getModelInternal</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.model;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return the underlying &lt;code&gt;ModelMap&lt;/code&gt; instance (never &lt;code&gt;null&lt;/code&gt;).</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> ModelMap <span class="title">getModelMap</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (<span class="keyword">this</span>.model == <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">this</span>.model = <span class="keyword">new</span> ModelMap();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.model;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return the model map. Never returns &lt;code&gt;null&lt;/code&gt;.</span></span><br><span class="line"><span class="comment">	 * To be called by application code for modifying the model.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Map&lt;String, Object&gt; <span class="title">getModel</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> getModelMap();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Add an attribute to the model.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> attributeName name of the object to add to the model</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> attributeValue object to add to the model (never &lt;code&gt;null&lt;/code&gt;)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> ModelMap#addAttribute(String, Object)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #getModelMap()</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> ModelAndView <span class="title">addObject</span><span class="params">(String attributeName, Object attributeValue)</span> </span>&#123;</span><br><span class="line">		getModelMap().addAttribute(attributeName, attributeValue);</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Add an attribute to the model using parameter name generation.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> attributeValue the object to add to the model (never &lt;code&gt;null&lt;/code&gt;)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> ModelMap#addAttribute(Object)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #getModelMap()</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> ModelAndView <span class="title">addObject</span><span class="params">(Object attributeValue)</span> </span>&#123;</span><br><span class="line">		getModelMap().addAttribute(attributeValue);</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Add all attributes contained in the provided Map to the model.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> modelMap a Map of attributeName -&gt; attributeValue pairs</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> ModelMap#addAllAttributes(Map)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #getModelMap()</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> ModelAndView <span class="title">addAllObjects</span><span class="params">(Map&lt;String, ?&gt; modelMap)</span> </span>&#123;</span><br><span class="line">		getModelMap().addAllAttributes(modelMap);</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Clear the state of this ModelAndView object.</span></span><br><span class="line"><span class="comment">	 * The object will be empty afterwards.</span></span><br><span class="line"><span class="comment">	 * &lt;p&gt;Can be used to suppress rendering of a given ModelAndView object</span></span><br><span class="line"><span class="comment">	 * in the &lt;code&gt;postHandle&lt;/code&gt; method of a HandlerInterceptor.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #isEmpty()</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> HandlerInterceptor#postHandle</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.view = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">this</span>.model = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">this</span>.cleared = <span class="keyword">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return whether this ModelAndView object is empty,</span></span><br><span class="line"><span class="comment">	 * i.e. whether it does not hold any view and does not contain a model.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">this</span>.view == <span class="keyword">null</span> &amp;&amp; CollectionUtils.isEmpty(<span class="keyword">this</span>.model));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return whether this ModelAndView object is empty as a result of a call to &#123;<span class="doctag">@link</span> #clear&#125;</span></span><br><span class="line"><span class="comment">	 * i.e. whether it does not hold any view and does not contain a model.</span></span><br><span class="line"><span class="comment">	 * &lt;p&gt;Returns &lt;code&gt;false&lt;/code&gt; if any additional state was added to the instance</span></span><br><span class="line"><span class="comment">	 * &lt;strong&gt;after&lt;/strong&gt; the call to &#123;<span class="doctag">@link</span> #clear&#125;.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@see</span> #clear()</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">wasCleared</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">this</span>.cleared &amp;&amp; isEmpty());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Return diagnostic information about this model and view.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		StringBuilder sb = <span class="keyword">new</span> StringBuilder(<span class="string">"ModelAndView: "</span>);</span><br><span class="line">		<span class="keyword">if</span> (isReference()) &#123;</span><br><span class="line">			sb.append(<span class="string">"reference to view with name '"</span>).append(<span class="keyword">this</span>.view).append(<span class="string">"'"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			sb.append(<span class="string">"materialized View is ["</span>).append(<span class="keyword">this</span>.view).append(<span class="string">']'</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		sb.append(<span class="string">"; model is "</span>).append(<span class="keyword">this</span>.model);</span><br><span class="line">		<span class="keyword">return</span> sb.toString();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sxt.web;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.ModelAndView;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.mvc.multiaction.MultiActionController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sxt.po.User;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user.do"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">MultiActionController</span>  </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@RequestMapping</span>(params=<span class="string">"method=reg"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> ModelAndView <span class="title">reg</span><span class="params">(String uname)</span></span>&#123;</span><br><span class="line">		ModelAndView mv = <span class="keyword">new</span> ModelAndView();</span><br><span class="line">		mv.setViewName(<span class="string">"index"</span>);</span><br><span class="line"><span class="comment">//		mv.setView(new RedirectView("index"));</span></span><br><span class="line">		</span><br><span class="line">		User u = <span class="keyword">new</span> User();</span><br><span class="line">		u.setUname(<span class="string">"高淇"</span>);</span><br><span class="line">		<span class="comment">//查看源代码，得知，直接放入对象。属性名为”首字母小写的类名”。 一般建议手动增加属性名称。</span></span><br><span class="line">		mv.addObject(u);   </span><br><span class="line">		mv.addObject(<span class="string">"a"</span>, <span class="string">"aaaa"</span>);</span><br><span class="line">		<span class="keyword">return</span> mv;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">%@</span> <span class="attr">page</span> <span class="attr">language</span>=<span class="string">"java"</span> <span class="attr">import</span>=<span class="string">"java.util.*"</span> <span class="attr">pageEncoding</span>=<span class="string">"gbk"</span>%&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">%@</span> <span class="attr">taglib</span> <span class="attr">prefix</span>=<span class="string">"c"</span> <span class="attr">uri</span>=<span class="string">"http://java.sun.com/jsp/jstl/core"</span> %&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">HTML</span> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//W3C//DTD HTML 4.01 Transitional//EN"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	   <span class="tag">&lt;<span class="name">h1</span>&gt;</span>$&#123;requestScope.a&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">	   <span class="tag">&lt;<span class="name">h1</span>&gt;</span>$&#123;requestScope.user.uname&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>地址栏输入：<a href="http://localhost:8080/springmvc03/user.do?method=reg" target="_blank" rel="noopener">http://localhost:8080/springmvc03/user.do?method=reg</a><br>结果为：<br>aaaa<br>高琪</p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-03-18T04:06:59.891Z" itemprop="dateUpdated">2020年3月18日 12:06</time>
</span><br>


        这里写留言或版权声明：<a href="/blog/2017/01/25/java/springmvc相关注解/" target="_blank" rel="external">http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/</a>
    </div>
    <footer>
        <a href="http://windynature.oschina.io">
            <img src="/blog/img/user2.jpg" alt="windy">
            windy
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/SpringMVC/">SpringMVC</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/&title=《Spring Web MVC相关注解》 — 奔跑的蜗牛&pic=http://windynature.oschina.io/img/user2.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/&title=《Spring Web MVC相关注解》 — 奔跑的蜗牛&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Spring Web MVC相关注解》 — 奔跑的蜗牛&url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/&via=http://windynature.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2017/01/26/java/Maven-Pom.xml文件详解/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Maven pom.xml文件详解</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2017/01/25/设计模式/适配器模式/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">适配器模式Adapter</h4>
      </a>
    </div>
  
</nav>



    

<div class="comments" id="comments">
    <div class="ds-thread" data-thread-key="java/springmvc相关注解" data-title="Spring Web MVC相关注解" data-url="http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/"></div>
</div>
<script>
lazyScripts.push('//cdn.bootcss.com/marked/0.3.6/marked.min.js');

var duoshuoQuery = {short_name:'ysblog', theme: 'none'};
lazyScripts.push('//unpkg.com/hexo-theme-material-indigo@1.5.2/js/embed.min.js');


</script>










</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>奔跑的蜗牛 &copy; 2015 - 2020</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/&title=《Spring Web MVC相关注解》 — 奔跑的蜗牛&pic=http://windynature.oschina.io/img/user2.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/&title=《Spring Web MVC相关注解》 — 奔跑的蜗牛&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Spring Web MVC相关注解》 — 奔跑的蜗牛&url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/&via=http://windynature.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://windynature.oschina.io/2017/01/25/java/springmvc相关注解/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: false };



</script>

<script src="//unpkg.com/hexo-theme-material-indigo@1.5.2/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@1.5.2/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
